/*
 * File: FindRange.java
 * Name: Diego A. Falciola
 * Section Leader: Sam King 
 * --------------------
 * This file is the starter file for the FindRange problem.
 */

import acm.program.*;

public class FindRange extends ConsoleProgram {
	private static final int SENTINEL = 0;
	
	// minimum value found 
	private int minimum;
	// maximum value found 
	private int maximum;
	
	// store wether the first value has been set or not
	private Boolean firstValueSet = false;
	
	
	/**
	 *  Reads values from user input
	 *  and prints minimum and maximum values found
	 */
	public void run() {
		readValues();
		
		printResults();
	}
	
	/**
	 *  Reads values from user input and set minimum and maximum  
	 */
	private void readValues(){
		println("This program finds the largest and smallest numbers.");
		
		int value = readValue();
		
		while(value!=SENTINEL){
			value = readValue();
			checkMinMax(value);
		}
	}
	
	/**
	 * Read value from user
	 * @return a number read from user input
	 */
	private int readValue(){
		int value = readInt("? ");
		
		// check for the first value entered
		// in order to initialize min and max
		if (value!=SENTINEL && !firstValueSet){
			minimum=maximum=value;
			firstValueSet=true;
		}
		
		return value;
	}
	
	/**
	 * Checks wether the number is minimum or maximum
	 * and updates internal values
	 * @param value a number to compare for min or max
	 */
	private void checkMinMax(int value){
		// return if SENTINEL found 
		if (value==SENTINEL){
			return;
		}
		
		// check for minimum value entered
		if (value<minimum){
			minimum = value;
		}
		
		// check for maximum value entered
		if (value>maximum){
			maximum=value;
		}
	}
	
	/**
	 *  Print results to the user 
	 */
	private void printResults(){
		if (!firstValueSet){
			println("No values");
			return;
		}
		
		println("min: " + minimum);
		println("max: " + maximum);
	}
}

